<template>
  <div class="approval-detail" v-loading="loading">
    <template v-if="detail">
      <!-- 基本信息 -->
      <el-descriptions title="申请信息" :column="2" border>
        <el-descriptions-item label="申请编号">{{ detail.id }}</el-descriptions-item>
        <el-descriptions-item label="申请类型">
          <el-tag>{{ detail.subTypeName }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ formatDateTime(detail.applyTime) }}</el-descriptions-item>
        <el-descriptions-item label="当前状态">
          <el-tag :type="getStatusType(detail.status)">{{ detail.statusName }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="当前层级" v-if="detail.status === 0">
          {{ detail.approvalLevelName }}
        </el-descriptions-item>
        <el-descriptions-item label="当前审批人" v-if="detail.status === 0">
          {{ detail.currentApproverName || '教务处' }}
        </el-descriptions-item>
      </el-descriptions>

      <!-- 学生信息快照 -->
      <el-descriptions title="学生信息" :column="2" border style="margin-top: 20px">
        <el-descriptions-item label="学号">{{ detail.studentNo }}</el-descriptions-item>
        <el-descriptions-item label="姓名">{{ detail.studentName }}</el-descriptions-item>
        <el-descriptions-item label="性别">{{ detail.gender === '1' ? '男' : '女' }}</el-descriptions-item>
        <el-descriptions-item label="院系">{{ detail.deptName }}</el-descriptions-item>
        <el-descriptions-item label="班级">{{ detail.className }}</el-descriptions-item>
        <el-descriptions-item label="入学年份">{{ detail.enrollYear }}</el-descriptions-item>
        <el-descriptions-item label="当前学籍状态">
          <el-tag>{{ detail.studentStatusName }}</el-tag>
        </el-descriptions-item>
      </el-descriptions>

      <!-- 申请内容 -->
      <el-card shadow="never" style="margin-top: 20px">
        <template #header>
          <span>申请内容</span>
        </template>
        <div class="content-text">{{ detail.content }}</div>
      </el-card>

      <!-- 转学目标信息 -->
      <el-descriptions 
        v-if="detail.subType === 'transfer'" 
        title="转学目标" 
        :column="2" 
        border 
        style="margin-top: 20px"
      >
        <el-descriptions-item label="目标院系">{{ detail.targetDeptName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="目标班级">{{ detail.targetClassName || '-' }}</el-descriptions-item>
      </el-descriptions>

      <!-- 休学时间 -->
      <el-descriptions 
        v-if="detail.subType === 'suspension' && detail.startDate" 
        title="休学时间" 
        :column="2" 
        border 
        style="margin-top: 20px"
      >
        <el-descriptions-item label="开始日期">{{ detail.startDate }}</el-descriptions-item>
        <el-descriptions-item label="结束日期">{{ detail.endDate }}</el-descriptions-item>
      </el-descriptions>

      <!-- 附件列表 -->
      <el-card v-if="detail.attachments && detail.attachments.length > 0" shadow="never" style="margin-top: 20px">
        <template #header>
          <span>附件材料</span>
        </template>
        <el-table :data="detail.attachments" border>
          <el-table-column prop="fileName" label="文件名" />
          <el-table-column prop="fileTypeName" label="文件类型" width="120" />
          <el-table-column prop="fileSizeFormatted" label="文件大小" width="120" />
          <el-table-column prop="createTime" label="上传时间" width="180">
            <template #default="{ row }">
              {{ formatDateTime(row.createTime) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template #default="{ row }">
              <el-button 
                type="primary" 
                size="small" 
                @click="previewFile(row)" 
                v-if="row.canPreview"
                link
              >
                预览
              </el-button>
              <el-button 
                type="success" 
                size="small" 
                @click="downloadFile(row)"
                link
              >
                下载
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 审批历史 -->
      <el-card v-if="detail.historyOpinions && detail.historyOpinions.length > 0" shadow="never" style="margin-top: 20px">
        <template #header>
          <span>审批记录</span>
        </template>
        <el-timeline>
          <el-timeline-item
            v-for="(opinion, index) in detail.historyOpinions"
            :key="index"
            :timestamp="formatDateTime(opinion.approveTime)"
            placement="top"
          >
            <el-card>
              <h4>{{ opinion.levelName }} - {{ opinion.approverName }}</h4>
              <p>
                <el-tag :type="getActionType(opinion.action)">{{ opinion.actionName }}</el-tag>
              </p>
              <p class="opinion-text">{{ opinion.opinion || '无' }}</p>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </el-card>

      <!-- 最终审批结果 -->
      <el-card v-if="detail.status !== 0" shadow="never" style="margin-top: 20px">
        <template #header>
          <span>最终审批结果</span>
        </template>
        <el-result
          :icon="detail.status === 1 ? 'success' : 'error'"
          :title="detail.status === 1 ? '申请已通过' : '申请被拒绝'"
        >
          <template #sub-title>
            <div>
              <p>审批人：{{ detail.approverName }}</p>
              <p>审批时间：{{ formatDateTime(detail.approveTime) }}</p>
              <p v-if="detail.remark">审批意见：{{ detail.remark }}</p>
            </div>
          </template>
        </el-result>
      </el-card>
    </template>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getApprovalDetail } from '@/api/approval'

const props = defineProps<{
  approvalId: number
}>()

const loading = ref(false)
const detail = ref<any>(null)

/**
 * 加载审批详情
 */
const loadDetail = async () => {
  loading.value = true
  try {
    const data = await getApprovalDetail(props.approvalId)
    detail.value = data
  } catch (error) {
    ElMessage.error('加载详情失败')
  } finally {
    loading.value = false
  }
}

/**
 * 获取状态类型
 */
const getStatusType = (status: number) => {
  const typeMap: Record<number, any> = {
    0: 'warning',
    1: 'success',
    2: 'danger'
  }
  return typeMap[status] || ''
}

/**
 * 获取操作类型
 */
const getActionType = (action: number) => {
  const typeMap: Record<number, any> = {
    1: 'success',
    2: 'danger',
    3: 'warning'
  }
  return typeMap[action] || ''
}

/**
 * 格式化日期时间
 */
const formatDateTime = (dateTime: string) => {
  if (!dateTime) return '-'
  return dateTime.replace('T', ' ').substring(0, 19)
}

/**
 * 预览文件
 */
const previewFile = (file: any) => {
  window.open(file.fileUrl, '_blank')
}

/**
 * 下载文件
 */
const downloadFile = (file: any) => {
  window.open(file.fileUrl, '_blank')
}

onMounted(() => {
  loadDetail()
})
</script>

<style scoped lang="scss">
.approval-detail {
  .content-text {
    line-height: 1.8;
    font-size: 14px;
    color: #606266;
    white-space: pre-wrap;
  }

  .opinion-text {
    margin: 10px 0 0;
    line-height: 1.6;
    color: #606266;
  }

  :deep(.el-descriptions__label) {
    font-weight: bold;
  }

  :deep(.el-timeline-item__timestamp) {
    color: #909399;
    font-size: 13px;
  }
}
</style>

